#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020-11-05 8:52 上午
# @Author  : yangdy
# @File    : 2020-11-05.127.单词接龙
# @Software: PyCharmCE
from tools.my_methods import *


class Solution:
    def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
        ans = 0
        try:
            end = wordList.index(endWord)
        except ValueError as err:
            return ans
        print(end)

        def cal(x, y):
            return sum(map(lambda xx: 0 if xx[0] == xx[1] else 1, zip(x, y)))

        mm = []
        for i in range(len(wordList)):
            mi = []
            for j in range(len(wordList)):
                if j == i:
                    continue

                if cal(wordList[i], wordList[j]) == 1:
                    mi.append((j, 1))
            mm.append(mi)
        print(mm)
        dis = []
        for i in range(len(wordList)):
            if wordList[i] == beginWord:
                dis.append(0)
            elif cal(beginWord, wordList[i]) == 1:
                dis.append(1)
            else:
                dis.append(-1)
        print(dis)
        si = [True for i in range(len(wordList))]
        for x in range(len(wordList)):
            print(x, dis)
            mi, m = -1, -1
            for i in range(len(wordList)):
                if si[i] and dis[i] != -1 and (mi == -1 or dis[i] < m):
                    mi, m = i, dis[i]
            print(mi, m)
            if mi < 0:
                break
            si[mi] = False
            for pi in mm[i]:
                if dis[i] + pi[1] < dis[pi[0]]:
                    dis[pi[0]] = dis[i] + pi[1]
        print(dis)
        ans = dis[end]

        return ans if ans > -1 else 0


questions = [
    ('hit', 'cog', ['hot', 'dot', 'dog', 'lot', 'log', 'cog']),
    ('hit', 'cog', ['hot', 'dot', 'dog', 'lot', 'log']),
]

work(questions, lambda q: Solution().ladderLength(q[0], q[1], q[2]))

